package com.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.app.common.Result;
import com.app.model.Prize;
import com.app.model.PrizeActivity;
import com.app.model.UserModel;
import com.app.service.IPrizeActivityService;
import com.app.service.IPrizeService;
import com.app.service.UserService;
import com.app.utils.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.time.LocalDateTime;

/**
 * <p>
 * 转盘活动 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2023-12-16
 */
@RestController
@RequestMapping("/activity")
public class PrizeActivityController {
    @Autowired
    private IPrizeActivityService iActivityService;
    @Autowired
    private IPrizeService iPrizeService;
    @Autowired
    private UserService iUserService;
    /**
     * 保存活动
     *
     * @param activity 活动
     * @return {@link Result}
     */
    @PostMapping("/admin/save")
    public Result saveActivity(@RequestBody PrizeActivity activity) {
        if (activity.getId() != null && activity.getId() > 0) {
            activity.setUpdateTime(LocalDateTime.now());
            return Result.success(this.iActivityService.updateById(activity));
        } else {
            activity.setCreateTime(LocalDateTime.now());
            return Result.success(this.iActivityService.save(activity));
        }
    }

    /**
     * 活动列表
     *
     * @param page         第页
     * @param limit        限制
     * @param activityName 活动名称
     * @return {@link Result}
     */
    @GetMapping("/admin/list")
    public Result activityList(Integer page, Integer limit, String activityName) {
        return Result.success(iActivityService.activityList(page, limit, activityName));
    }

    /**
     * 活动删除
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/admin/delete")
    public Result activityDelete(Integer id) {
        if (this.iActivityService.removeById(id)){
            QueryWrapper<Prize> priceQueryWrapper = new QueryWrapper<>();
            priceQueryWrapper.eq("game_id",id);
            iPrizeService.remove(priceQueryWrapper);
        }
        return Result.success("");
    }

    /**
     * 活动详细信息
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/admin/details")
    public Result activityDetails(Integer id) {
        return Result.success(this.iActivityService.getById(id));
    }

    /**
     * 活动状态
     *
     * @param id             id
     * @param status 活动状态
     * @return {@link Result}
     */
    @GetMapping("/admin/status")
    public Result activityStatus(Integer id, Integer status) {
        PrizeActivity byId = this.iActivityService.getById(id);
        if (byId != null) {
            byId.setStatus(status);
        }
        return Result.success(this.iActivityService.updateById(byId));
    }

    /**
     * 小程序:获取活动列表
     *
     * @return {@link Result}
     * @throws ParseException 解析异常
     */
    @GetMapping("/list")
    public Result getActivityList(String activityName, HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = iUserService.getUserByOpenid(openId);
        Integer userId = userModel.getId();
        return Result.success(iActivityService.getActivityList(userId,activityName));
    }

    /**
     * 抽奖活动
     *
     * @return {@link Result}
     */
    @GetMapping("/raffle")
    @Transactional(rollbackFor = Exception.class)
    public Result raffle(Integer activityId, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = iUserService.getUserByOpenid(openId);
        Object raffle = iActivityService.raffle(userModel,activityId);
        if(ObjectUtil.toString(raffle).equals("益联券不足"))
        {
            return Result.error(201,"益联券不足");
        } else if (ObjectUtil.toString(raffle).equals("积分不足")) {
            return Result.error(201,"积分不足");
        }
        return Result.success(raffle);
    }


}

